//
//  NewsCommentTool.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/12.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NewsCommentTool.h"
#import "UIView+WZLBadge.h"

@interface NewsCommentTool ()
@property(nonatomic, strong)UIView *contentView;
@property(nonatomic, strong)UIView *topLine;
@property(nonatomic, strong)UIView *textFiledView;
@property(nonatomic, strong)UIButton *leftView;

@property(nonatomic, strong)UIButton *commentBtn;
@property(nonatomic, strong)UIButton *collectBtn;
@property(nonatomic, strong)UIButton *shareBtn;

@end

@implementation NewsCommentTool

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.contentView = [[UIView alloc] init];
        [self addSubview:self.contentView];
        
        self.topLine = [[UIView alloc] init];
        self.topLine.backgroundColor = [[UIColor xm_somberColor] colorWithAlphaComponent:0.4];
        [self addSubview:self.topLine];
        
        self.textFiledView = [[UIView alloc] init];
        self.textFiledView.backgroundColor = [UIColor xm_colorFromRGB:0xF7F8FA];
        self.textFiledView.clipsToBounds = YES;
        self.textFiledView.layer.cornerRadius = 15.0;
        [self.contentView addSubview:self.textFiledView];
        
        self.leftView = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.leftView setImage:[UIImage imageNamed:@"textField_shape"] forState:UIControlStateNormal];
        [self.leftView setTitle:@" 写评论..." forState:UIControlStateNormal];
        self.leftView.userInteractionEnabled = NO;
        [self.leftView setTitleColor:[UIColor xm_somberColor] forState:UIControlStateNormal];
        self.leftView.titleLabel.font = [UIFont xm_font:13.0];
        [self.textFiledView addSubview:self.leftView];
        
        self.commentBtn = [self buttonWithTag:0 image:@"textField_shape"];
        self.collectBtn = [self buttonWithTag:1 image:@"textField_shape"];
        self.shareBtn   = [self buttonWithTag:2 image:@"textField_shape"];
        
        self.commentBtn.badgeCenterOffset = CGPointMake(25, 6);
        [self.commentBtn showNumberBadgeWithValue:14];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tTapGestureRecognizer)];
        [self.textFiledView addGestureRecognizer:tapGesture];
    }
    return self;
}
-(void)tTapGestureRecognizer{
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentToolTextViewClick)]) {
        [self.delegate commentToolTextViewClick];
    }
}
-(UIButton *)buttonWithTag:(NSInteger)tag image:(NSString *)image{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    button.tag = tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
    return button;
}
-(void)buttonClick:(UIButton *)sender{
    if (sender.tag == 1) {//收藏
        [self collectArticle];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentToolSelectedIndex:)]) {
        [self.delegate commentToolSelectedIndex:sender.tag];
    }
}
-(void)setModel:(AticleDetailModel *)model{
    _model = model;
}
- (void)collectArticle{
    NSDictionary * dict = @{@"ID":@(self.model.ID)};
    [FBNetworkTool get:@"articleapi/com.CollectionArticles" parameters:dict showHUD:NO success:^(XMHttpResponseModel *responseModel) {
        [MBProgressHUD showSuccess:responseModel.msg];

        NSLog(@"responseModel == %@",responseModel);
    } failure:^(NSError *error) {
        
    }];
}




-(void)layoutSubviews{
    [super layoutSubviews];
    self.topLine.frame = CGRectMake(0, 0, self.width, 0.5);
    self.contentView.frame = CGRectMake(0, 0.5, self.width, 48);
    self.textFiledView.frame = CGRectMake(15, 9, self.width - 30 - 120, 30);
    
    self.leftView.x = 10;
    self.leftView.y = 0;
    self.leftView.size = CGSizeMake(80, 30);
    
    self.commentBtn.size = self.collectBtn.size = self.shareBtn.size = CGSizeMake(30, 30);
    self.commentBtn.y = self.collectBtn.y = self.shareBtn.y = 9;
    self.commentBtn.x = self.width - 120;
    self.collectBtn.x = self.width - 80;
    self.shareBtn.x   = self.width - 40;

}
-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
}


@end
